package com.hbx.eduorder.service.impl;

import com.hbx.commonutils.ordervo.CourseWebVoOrder;
import com.hbx.commonutils.ordervo.UcenterMemberOrder;
import com.hbx.eduorder.client.EduClient;
import com.hbx.eduorder.client.UcenterClient;
import com.hbx.eduorder.entity.Order;
import com.hbx.eduorder.mapper.OrderMapper;
import com.hbx.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String memberId) {
        //通过远程调用根据用户id获取用户信息
        CourseWebVoOrder courseDto = eduClient.getCourseInfoOrder(courseId);
        //通过远程调用根据课程id获取课程信息
        UcenterMemberOrder ucenterMember = ucenterClient.getUserInfoOrder(memberId);

        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0); //未支付为0 支付了为1
        order.setPayType(1); //1 为微信支付 2 为支付宝支付

        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
